function debounce<A extends any[], R>(
  fn: (...args: A) => R,
  delay?: number,
): (...args: A) => void {
  let timer;
  return function () {
    const _this = this;
    const args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn.apply(_this, args);
    }, delay);
  };
}

export default debounce;
